package actions;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Applicant;
import model.Database;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DbException;

public class StudentLoginAction extends Action{
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws DbException, SQLException, ClassNotFoundException{
		
		Class.forName("org.postgresql.Driver");
		String database = "postgres";
		String username = "postgres";
		String password = "postgres";
		// make a connection to the database itself
		Connection conn = DriverManager.getConnection("jdbc:postgresql:"
				+ database, username, password);
		Statement stmt = conn.createStatement();
		ResultSet rs = stmt.executeQuery("SELECT degree.id FROM public.degree, public.applicant WHERE degree.\"owner\" = applicant.pid AND applicant.id = "+ Database.getIdByUsername(request.getUserPrincipal().getName())+";");
		rs.next();
		try{
			rs.getInt(1);
		}catch(SQLException e){
			return mapping.findForward("notSubmitted");
		}
		
		Applicant user = Database.getApplicantById(Database.getIdByUsername(request.getUserPrincipal().getName()));
		
		
		return mapping.findForward("submitted");
		
	}

}
